package member

import (
	"parking-lot-go-dingding/controller"
	"parking-lot-go-dingding/model/cars"
	"parking-lot-go-dingding/model/view"
	"strings"

	"github.com/gin-gonic/gin"
)

func Detail(c *gin.Context) {
	requestStruct := struct {
		Id string `json:"id"`
	}{}

	//接收加密串
	if err := c.ShouldBindJSON(&requestStruct); err != nil {
		controller.Fail(c, "json接收失败"+err.Error())
		return
	}

	var membersDingtalkViewStruct view.MembersDingtalkViewStruct
	memberFind, err := membersDingtalkViewStruct.First(map[string]interface{}{
		"member_id": requestStruct.Id,
	})
	if err != nil {
		controller.Fail(c, "会员查询失败"+err.Error())
		return
	}

	var carsStruct cars.CarsStruct
	cars, err := carsStruct.List(map[string]interface{}{
		"member_id": requestStruct.Id,
	})
	if err != nil {
		controller.Fail(c, "会员查询失败"+err.Error())
		return
	}

	carsNumbers := ""
	carsInfos := ""
	carIds := []string{}
	for _, v := range cars {
		carsNumbers += v.CarNumber + "\n"
		carsInfos += v.CarInfo + "\n"
		carIds = append(carIds, v.CarNumber)
	}
	carsNumbers = strings.TrimRight(carsNumbers, "\n")
	carsInfos = strings.TrimRight(carsInfos, "\n")

	returnData := map[string]interface{}{
		"id":              memberFind.MemberId,
		"real_name":       memberFind.MemberRealName,
		"mobile":          memberFind.MemberMobile,
		"expires_time":    memberFind.MemberExpiresTime.Format("2006-01-02 15:04"),
		"money":           memberFind.MemberMoney,
		"remarks":         memberFind.MemberRemarks,
		"car_numbers":     carsNumbers,
		"car_info":        carsInfos,
		"car_id":          carIds,
		"cars":            cars,
		"dingtalk_userid": memberFind.MemberDingtalkUserid,
		"department_name": memberFind.MemberDingtalkDepartmentName,
		"position_name":   memberFind.MemberDingtalkPosition,
	}

	controller.Success(c, returnData)
}
